package gdut.jmxywz.action;

import gdut.jmxywz.been.BaseFunction;
import gdut.jmxywz.been.RoleFunction;
import gdut.jmxywz.util.PageModel;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class baseFunctionAction extends BaseAction {
	private BaseFunction baseFunction;
	
	private BaseFunction parentFunction;
	
	private BaseFunction baseFunctionDetail;
	
	private BaseFunction baseFunctionUpdate;
	
	private PageModel pageModel;
	
	private List baseFunctionList;
	
	private List baseFunctionUpdateList;
	
	private int FunctionId;
	
	/***************分页*****************/
	private int currentPage=1;
	
	private int pageSize=5;
	
	/***************检索*****************/
	private String searchSelect="";
	
	private String like="like";
	
	private String searchValue="";
	
	
	
	public BaseFunction getBaseFunction() {
		return baseFunction;
	}

	public void setBaseFunction(BaseFunction baseFunction) {
		this.baseFunction = baseFunction;
	}
	
	
	public BaseFunction getBaseFunctionDetail() {
		return baseFunctionDetail;
	}

	public void setBaseFunctionDetail(BaseFunction baseFunctionDetail) {
		this.baseFunctionDetail = baseFunctionDetail;
	}
	
	

	public BaseFunction getBaseFunctionUpdate() {
		return baseFunctionUpdate;
	}

	public void setBaseFunctionUpdate(BaseFunction baseFunctionUpdate) {
		this.baseFunctionUpdate = baseFunctionUpdate;
	}

	public PageModel getPageModel() {
		return pageModel;
	}

	public void setPageModel(PageModel pageModel) {
		this.pageModel = pageModel;
	}
	
	public List getBaseFunctionList() {
		return baseFunctionList;
	}

	public void setBaseFunctionList(List baseFunctionList) {
		this.baseFunctionList = baseFunctionList;
	}
	
	
	public int getFunctionId() {
		return FunctionId;
	}

	public void setFunctionId(int functionId) {
		FunctionId = functionId;
	}
	

	public List getBaseFunctionUpdateList() {
		return baseFunctionUpdateList;
	}

	public void setBaseFunctionUpdateList(List baseFunctionUpdateList) {
		this.baseFunctionUpdateList = baseFunctionUpdateList;
	}

	/***************分页get,set*****************/
	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	/***************检索get,set*****************/
	public String getSearchSelect() {
		return searchSelect;
	}

	public void setSearchSelect(String searchSelect) {
		this.searchSelect = searchSelect;
	}

	public String getLike() {
		return like;
	}

	public void setLike(String like) {
		this.like = like;
	}

	public String getSearchValue() {
		return searchValue;
	}

	public void setSearchValue(String searchValue)throws UnsupportedEncodingException {
		this.searchValue = new String(searchValue.getBytes("ISO-8859-1"),"utf-8");
	}

	public BaseFunction getParentFunction() {
		return parentFunction;
	}

	public void setParentFunction(BaseFunction parentFunction) {
		this.parentFunction = parentFunction;
	}
	
	/*############################################################################################*/
	public String preAdd() throws Exception {
		// TODO Auto-generated method stub
		/*为新增页面查询备选的父功能节点*/
		String hql="from BaseFunction";
		baseFunctionList=new ArrayList();
		baseFunctionList=this.getCommonService().findListByHql(hql, null);
		if(baseFunctionList==null){
			return "error";
		}
		return SUCCESS;	
		
		
	}
	
	public String add() throws Exception {
		// TODO Auto-generated method stub
		/*判断是不是增加父节点，如果是则功能外键为空*/
		if(baseFunction.getBaseFunction().getFunctionId()==0){
			baseFunction.setBaseFunction(null);
		}
		System.out.println("============================================"+baseFunction.getDisplayOrder());
		boolean bl=this.getCommonService().saveObject(baseFunction);
		if(bl)
			return SUCCESS;
		else
			return "error";
	}
	
	public String show()throws Exception{
		String hql="from BaseFunction";
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"baseFunctionShow.action","");
		if(pageModel.getList()==null){
			System.out.println("=================功能列表为空================");
		}
		return SUCCESS;
	}

	
	public String search() throws Exception {
		// TODO Auto-generated method stub
		String hql="from BaseFunction";
		String searchParameter="";
		
		String searchValue1=searchValue;
		
		if (like.equals("like")) {
			hql = hql + " as bf  where bf." + searchSelect + " like '%"+ searchValue1 + "%'";
		} else {
			hql = hql + " as bf  where bf." + searchSelect + " ='"+ searchValue1 + "'";
		}
	
		String enSearchValue=java.net.URLEncoder.encode(searchValue1,"UTF-8");//URL编码转换
		
		searchParameter="&searchSelect="+searchSelect+"&searchValue="+enSearchValue+"&like="+like;
		
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"baseFunctionSearch.action",searchParameter);
		System.out.print(hql);
		System.out.print(pageModel);
		
		return SUCCESS;
		
	}

	
	public String detail() throws Exception {
		// TODO Auto-generated method stub
		
		baseFunctionDetail=(BaseFunction)this.getCommonService().findByIntId(FunctionId, "gdut.jmxywz.been.BaseFunction");
		
		if(baseFunctionDetail==null)
			return "error";
		else
			return SUCCESS;
	}


	public String preUpdate() throws Exception {
		// TODO Auto-generated method stub
		baseFunctionUpdate=(BaseFunction)this.getCommonService().findByIntId(FunctionId, "gdut.jmxywz.been.BaseFunction");
		
		String hql="from BaseFunction";
		baseFunctionUpdateList=new ArrayList();
		baseFunctionUpdateList=this.getCommonService().findListByHql(hql, null);
		
		if(baseFunctionUpdate==null)
			return "error";
		
		return SUCCESS;
	}

	public String update() throws Exception {
		// TODO Auto-generated method stub
		boolean bl=this.getCommonService().updateObject(baseFunctionUpdate);
		if(bl)
			return SUCCESS;
		else
			return "error";
	}
	
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		/*删除功能前先删除角色功能表中的关联*/
		String deleteList="select rf from RoleFunction rf where rf.baseFunction.FunctionId="+FunctionId;
		List deleteRfList=this.getCommonService().findListByHql(deleteList, null);
		for(int i=0;i<deleteRfList.size();i++){
			boolean d=this.getCommonService().deleteObject((RoleFunction)deleteRfList.get(i));
			if(!d){
				return "error";
			}
		}
		/*删除功能*/
		baseFunction=(BaseFunction)this.getCommonService().findByIntId(FunctionId, "gdut.jmxywz.been.BaseFunction");
		if(baseFunction!=null){
			boolean bl=this.getCommonService().deleteObject(baseFunction);
			return SUCCESS;
		}else{
				return "error";
			}
		
	}
	
	
	
}
